// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// q [author] rhyme_lph
// q [email]  rhymelph@gmail.com
// s [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/entities/ocr_attachment_entity.dart';
import 'package:finance_app/src/provider/dashboard/products_services/adjust_inventory_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:finance_app/src/utils/date_time_utils.dart';
import 'package:finance_app/src/utils/object_utils.dart';
import 'package:finance_app/src/widgets/button/normal_text_button.dart';
import 'package:finance_app/src/widgets/common/normal_error_text.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/form/adjust_number_form.dart';
import 'package:finance_app/src/widgets/form/attach_file_form.dart';
import 'package:finance_app/src/widgets/form/text_area_input_form.dart';
import 'package:finance_app/src/widgets/form/value_select_form.dart';
import 'package:finance_app/src/widgets/wrapper/normal_form_wrapper.dart';
import 'package:flutter/material.dart';
import 'package:tuple/tuple.dart';

@RouterMeta(paramName: "adjustInventoryPage")
class AdjustInventoryPage extends StatefulWidget {
  final int id;
  final String quantity;
  final String productName;

  const AdjustInventoryPage({Key key, this.id, this.productName, this.quantity})
      : super(key: key);

  @override
  _AdjustInventoryPageState createState() => _AdjustInventoryPageState();
}

class _AdjustInventoryPageState extends State<AdjustInventoryPage>
    with PagePresenter<AdjustInventoryPage, AdjustInventoryProvider> {
  @override
  void initState() {
    provider.initQuantity = int.parse(widget.quantity);
    provider.updateQuantity = int.parse(widget.quantity);
    provider.id = widget.id;
    super.initState();
  }

  @override
  PreferredSizeWidget get appBar =>
      getDefaultAppBar(title: S.current.adjust_inventory, actions: [
        NormalSaveTextButton(
          onTap: provider.onSave,
        )
      ]);

  @override
  Widget get buildWidget => ColoredBox(
        color: Colors.white,
        child: ListView(
          children: [
            listBuilder<DateTime>(
              selector: (_, __) => __.date,
              builder: (BuildContext context, DateTime value, Widget child) =>
                  ValueSelectForm(
                title: S.current.date,
                value: ObjectUtils.isNullDefaultFormat<DateTime>(value, null,
                    (data) => DateTimeUtils.formatGlobalDateTime(data)),
                onTap: provider.onChangeIssueDate,
              ),
            ),
            listBuilder<bool>(
                selector: (_, __) => __.currentSelect == 'Return Item',
                builder: (BuildContext context, bool value, Widget child) =>
                    Visibility(visible: value, child: child),
                child: listBuilder<String>(
                  selector: (_, __) => __.invoiceEntity?.number,
                  builder: (BuildContext context, String value, Widget child) =>
                      ValueSelectForm(
                    title: S.current.invoice_number,
                    value: value,
                    onTap: provider.onSelectInvoice,
                  ),
                )),
            listBuilder<Tuple2<int, int>>(
              selector: (_, __) => Tuple2(__.initQuantity, __.updateQuantity),
              builder: (BuildContext context, Tuple2<int, int> value,
                      Widget child) =>
                  AdjustNumberForm(
                title: '${widget.productName ?? ''}',
                content: S.current.stock_(value.item1),
                numberTitle: S.current.actual,
                initialNumber: value.item1,
                otherValue: value.item2 - value.item1,
                onNumberCallback: provider.onChangeNumber,
              ),
            ),
            AppSpaces.grayH05,
            listBuilder<bool>(
              selector: (_, __) =>
                  __.updateQuantity < __.initQuantity &&
                  __.currentSelect == 'Return Item',
              builder: (BuildContext context, value, Widget child) => value
                  ? NormalErrorText(
                      isVisibility: value,
                      content:
                          S.current.actual_quantity_cannot_be_less_than_stock,
                    )
                  : Container(),
            ),
            TextAreaInputForm(
              title: S.current.additional_notes_optional,
              controller: provider.additionalController,
              focusNode: provider.additionalFocusNode,
            ),
            NormalFormWrapper(
              child:
                  listBuilder<Tuple2<List<String>, List<OcrAttachmentEntity>>>(
                selector: (_, __) => Tuple2(
                    List.from(__.files), List.from(__.attachments ?? [])),
                builder: (BuildContext context,
                        Tuple2<List<String>, List<OcrAttachmentEntity>> value,
                        Widget child) =>
                    AttachFileForm(
                  onAdd: provider.onAddAttachment,
                  files: value.item1,
                  onSelectFile: provider.onSelectFile,
                  attachments: value.item2,
                  onSelectAttachment: provider.onSelectAttachment,
                ),
              ),
            ),
          ],
        ),
      );
}
